﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using DragDraw.Util;
using DragDraw.UserService;

namespace DragDraw.Tool
{
    public partial class AlbumBoard : UserControl
    {
        private List<Album> m_albums;
        private WidgetServiceProxy m_proxy;

        private int m_curIndex;
        private int m_curAlbum;

        public AlbumBoard()
        {
            InitializeComponent();
        }

        void OnGetSession(object sender, DragDraw.UserService.GetSessionCompletedEventArgs e)
        {
            XmlSerializer xs = new XmlSerializer(typeof(AlbumList));
            XmlReader xr = XmlReader.Create(new StringReader(e.Result));
            AlbumList al = (AlbumList)xs.Deserialize(xr);
            m_albums = al.Albums;
            m_curAlbum = 0;
            m_curIndex = 0;
            imgMain.Source = new BitmapImage(new Uri(m_albums[m_curAlbum].Photos[m_curIndex].ImgUrl));
            txtTitle.Text = m_albums[m_curAlbum].Photos[m_curIndex].Name;
        }

        /*
        void OnSaveFile(object sender, UploadFileCompletedEventArgs e)
        {
            if (e.Error == null)
                txtTitle.Text = "upload success!";
            else
                txtTitle.Text = e.Error.Message;
        }
         * */

        private void btnPre_Click(object sender, RoutedEventArgs e)
        {
            int count = m_albums[m_curAlbum].Photos.Count;
            m_curIndex = (m_curIndex + count - 1) % count;
            imgMain.Source = new BitmapImage(new Uri(m_albums[m_curAlbum].Photos[m_curIndex].ImgUrl));
            txtTitle.Text = m_albums[m_curAlbum].Photos[m_curIndex].Name;
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            int count = m_albums[m_curAlbum].Photos.Count;
            m_curIndex = (m_curIndex + count + 1) % count;
            imgMain.Source = new BitmapImage(new Uri(m_albums[m_curAlbum].Photos[m_curIndex].ImgUrl));
            txtTitle.Text = m_albums[m_curAlbum].Photos[m_curIndex].Name;
        }

        private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                Filter = "Jpeg Files (*.jpg)|*.jpg|All Files(*.*)|*.*",
            };
            if (openFileDialog.ShowDialog() == true)
            {
                //m_proxy.SaveFile(openFileDialog.SelectedFile.Name, openFileDialog.SelectedFile.OpenRead());
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            m_proxy = OperationManager.Instance.CreateWidgetServiceProxy(this);
            m_proxy.GetSessionCompleted += OnGetSession;
            //m_proxy.OnSaveFile += OnSaveFile;

            m_proxy.GetSession("Photos");
        }
    }

    public class Photo
    {
        public string ImgUrl;
        public string Name;
    }

    public class Album
    {
        public string AlbumName;
        public List<Photo> Photos;
    }

    public class AlbumList
    {
        public List<Album> Albums;
    }
}
